import 'package:app/pages/chat/chat_controller.dart';
import 'package:app/pages/chat/widget/mock.dart';
import 'package:app/shared/constants/common.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'chat_input_field.dart';
import 'message.dart';

class ChatBody extends StatefulWidget {
  ChatBody({Key? key}) : super(key: key);

  @override
  _ChatBodyState createState() => _ChatBodyState();
}

class _ChatBodyState extends State<ChatBody> {
  final ChatController _controller = Get.find<ChatController>();
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        Column(
          children: [
            Expanded(
              child: Padding(
                  padding: EdgeInsets.symmetric(
                      horizontal: sDefaultMarginPadding * 2),
                  child: ListView.builder(
                      itemCount: demeChatMessages.length,
                      itemBuilder: (context, index) => ChatMessageWidget(
                            message: demeChatMessages[index],
                          ))),
            ),
            ChatInputField()
          ],
        ),
        GetBuilder(
            init: _controller,
            builder: (controller) {
              print(_controller.dbLevel);
              return _controller.showLcp
                  ? Positioned(
                      bottom: 100,
                      child: Container(
                        alignment: Alignment.bottomCenter,
                        height: 80,
                        width: MediaQuery.of(context).size.width,
                        child: CustomPaint(
                            size: Size(double.maxFinite, 100),
                            painter: LCPainter(
                                amplitude: _controller.dbLevel / 2,
                                number: 30 - _controller.dbLevel ~/ 20)),
                      ))
                  : Container();
            })
      ],
    );
  }
}

class LCPainter extends CustomPainter {
  final double amplitude;
  final int number;
  LCPainter({this.amplitude = 100.0, this.number = 20});
  @override
  void paint(Canvas canvas, Size size) {
    var centerY = 0.0;
    var width = 400 / number;

    for (var a = 0; a < 4; a++) {
      var path = Path();
      path.moveTo(0.0, centerY);
      var i = 0;
      while (i < number) {
        path.cubicTo(width * i, centerY, width * (i + 1),
            centerY + amplitude - a * (30), width * (i + 2), centerY);
        path.cubicTo(width * (i + 2), centerY, width * (i + 3),
            centerY - amplitude + a * (30), width * (i + 4), centerY);
        i = i + 4;
      }
      canvas.drawPath(
          path,
          Paint()
            ..color = a == 0 ? Colors.blueAccent : Colors.blueGrey.withAlpha(50)
            ..strokeWidth = a == 0 ? 3.0 : 2.0
            ..maskFilter = MaskFilter.blur(
              BlurStyle.solid,
              5,
            )
            ..style = PaintingStyle.stroke);
    }
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) {
    return true;
  }
}
